﻿using System;
using System.Drawing;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;
using System.Globalization;

namespace ContractManagementSystem
{
    public partial class ViewContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                AddValueControl();
            }
        }

        private void AddValueControl()
        {
            ContractTypeDropDownList.Items.Add("- Choose ContractType -");
            //var contractTypeBusiness = new ContractTypeBusiness();
            ContractTypeDropDownList.DataSource = ContractTypeBusiness.FetchAll();
            ContractTypeDropDownList.DataTextField = "Name";
            ContractTypeDropDownList.DataValueField = "ContractTypeId";
            ContractTypeDropDownList.DataBind();

            ManagerDropDownList.Items.Add("- Choose Manager -");
            //var lineManagerBusiness = new LineManagerBussiness();
            ManagerDropDownList.DataSource = LineManagerBusiness.FetchAll(1);
            ManagerDropDownList.DataTextField = "FullName";
            ManagerDropDownList.DataValueField = "ManagerId";
            ManagerDropDownList.DataBind();

            ConsultantDropDownList.Items.Add("- Choose Consultant -");
            //var consultantBusiness = new ConsultantBussiness();
            ConsultantDropDownList.DataSource = ConsultantBusiness.FetchAll(1);
            ConsultantDropDownList.DataTextField = "FullName";
            ConsultantDropDownList.DataValueField = "ConsultantId";
            ConsultantDropDownList.DataBind();

        }

        protected void AddButtonClick(object sender, EventArgs e)
        {
            var contractorEntity = new ContractorEntity
            {
                UserId = UserBusiness.InitialUser(FullNameTextBox.Text, "Contractor"),
                Birthday = DateTime.Parse(BirthDateTextBox.Text,CultureInfo.CurrentCulture),
                FullName = FullNameTextBox.Text,
                Gender = GenderDropDownList.SelectedIndex,
                Email = EmailTextBox.Text,
                Address = AddressTextBox.Text,
                PhoneNumber1 = PhoneNumberTextBox.Text,
                PhoneNumber2 = PhoneNumbersTextBox.Text,
                Branch = BranchTextBox.Text
            };
            MessageLabel.Text = ContractorBusiness.Insert(contractorEntity) ? "Insert Success !" : "Insert False !";

            var userName = UserBusiness.FetchUserNameById(contractorEntity.UserId);

            var contractorId = UserBusiness.FetchIdByUserName(userName);
            var contractTypeId = new Guid(ContractTypeDropDownList.SelectedValue);
            var managerId = new Guid(ManagerDropDownList.SelectedValue);
            var consultantId = new Guid(ConsultantDropDownList.SelectedValue);
            var startDate = StartDateTextbox.Text;
            var endDate = EndDateTextbox.Text;
            var content = ContentTextbox.Text;

            var entity = new ContractEntity
                                        {
                                            ContractorId = new Guid(contractorId),
                                            ContractTypeId = contractTypeId,
                                            ManagerId = managerId,
                                            ConsultantId = consultantId,
                                            StartDate = DateTime.Parse(startDate,CultureInfo.CurrentCulture),
                                            EndDate = DateTime.Parse(endDate, CultureInfo.CurrentCulture),
                                            Content = content
                                        };
            if (ContractBusiness.Insert(entity))
            {
                Response.Redirect("ViewContract.aspx");
            }
            else
            {
                MessageLabel.ForeColor = Color.Red;
                MessageLabel.Text = "Failure !";
            }

            
        }


    }
}
